package com.woniuxy.userassembly.apiimpl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.api.RbacPermissionApi;
import com.woniuxy.common.PageResult;
import com.woniuxy.common.Result;
import com.woniuxy.form.RbacPermissionForm;
import com.woniuxy.userassembly.model.RbacPermission;
import com.woniuxy.userassembly.model.RbacRole;
import com.woniuxy.userassembly.service.IRbacPermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Liyfi
 * @since 2021-05-22
 */
@RestController
public class RbacPermissionController implements RbacPermissionApi {
    @Autowired
    private IRbacPermissionService permissionService;



    @Override
    public Result addPermission(RbacPermissionForm form) {
        RbacPermission permission = new RbacPermission();
        BeanUtils.copyProperties(form,permission);
        int i = permissionService.addPermission(permission);
        if(i == 1){
            return Result.success("新增成功");
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result selPermission(int permissionId) {
        RbacPermission permission = permissionService.selPermission(permissionId);
        if(permission != null){
            return Result.success(permission);
        }
        return Result.fail("查询失败");
    }

    @Override
    public PageResult findAllPermission(int pageIndex) {
        Page<RbacPermission> pageParam = new Page<>(pageIndex,10L);
        IPage<RbacPermission> pageResult = permissionService.page(pageParam);
        PageResult<RbacPermission> myPageResult = new PageResult<RbacPermission>((int) pageResult.getTotal(), (int) pageResult.getCurrent(), (int) pageResult.getSize(), pageResult.getRecords());
        myPageResult.setSuccess(true);
        return myPageResult;
    }
}

